var lastId;

$(function () {
    initId();
    $("#1_1").focus();
});

/* 删除本行 */
function deleteCurrentRow(obj) {
    if (confirm("确定删除?")) {
        $(obj).parents('tr').remove();
        initId();
    }
}

/* 添加到本行前 */
function addBefore(obj) {
    var tr = $(obj).parents('tr');
    tr.clone().insertBefore(tr);
    initId();
}

/* 添加到本行之后 */
function addLater(obj) {
    var tr = $(obj).parents('tr');
    tr.clone().insertAfter(tr);
    initId();
}

function submitAction(obj) {
    if (UTIL.checkButton(obj)) {
        var errMsg = checkVocabulary();
        if (errMsg) {
            alert(errMsg);
        } else {
            var formData = $('#actionSubmit').serialize();
            API.checkSentenceEdit(formData, function (res) {
                if (res.code == '0') {
                    window.location.href = "/check_sentence/list.htm";
                    return true;
                } else if (res.code == '30001') {
                    if (confirm(res.desc)) {
                        API.forceEditSentence(res.data, function (res) {
                            if (res.code == '0') {
                                window.location.href = "/check_sentence/list.htm";
                                return true;
                            } else {
                                alert(res.desc);
                            }
                        });
                    }
                } else {
                    alert(res.desc);
                }
                UTIL.releaseButton(obj);
            });
        }
        return false;
    }
}

function checkVocabulary() {
    var errMsg = "";
    var vs = $("#actionSubmit input[name=vocabulary]");
    var sentence = $("#sentence").val();
    var vStr = '';
    for (var i = 0; i < vs.length; i++) {
        if (!$.trim($(vs[i]).val())) {
            return "分词不要有空值";
        }
        vStr += $.trim($(vs[i]).val());
    }
    if (vStr != sentence) {
        errMsg = "分词和句子不匹配";
    }
    return errMsg;
}

// 预览组词效果
function preview(obj) {
    if (UTIL.checkButton(obj)) {
        var errMsg = checkVocabulary();
        if (errMsg) {
            alert(errMsg);
        } else {
            var formData = $('#actionSubmit').serialize();
            API.analysisSentencePreview(formData, function (res) {
                if (res.code == '0') {
                    var preview = res.data.preview;
                    $("#preview span").html(preview);
                    $("#preview").show();
                    return true;
                } else {
                    alert(res.desc);
                }
            });
        }
        UTIL.releaseButton(obj);
        return false;
    }
}

function fastSkip(obj) {
    var code = window.event.keyCode;
    var idArr = $(obj).attr("id").split("_");
    var id1 = parseInt(idArr[0]);
    var id2 = parseInt(idArr[1]);
    // 当焦点在提交按钮上时，向上则表示回到最后一行数据
    if (id1 == 0 && code == 38) {
        $("#" + lastId).focus();
        return;
    }

    var next;
    // ctrl向左
    if (code == 37) {
        // 禁止下拉框中的值切换
        event.returnValue = false;
        next = $("#" + id1 + "_" + --id2);
    }
    // ctrl向上
    if (code == 38) {
        event.returnValue = false;
        next = $("#" + --id1 + "_" + id2);
    }
    // ctrl向右
    if (code == 39) {
        event.returnValue = false;
        next = $("#" + id1 + "_" + ++id2);
    }
    // ctrl向下
    if (code == 40) {
        event.returnValue = false;
        next = $("#" + ++id1 + "_" + id2);
    }

    if (next) {
        if (next.attr("id")) {
            next.focus();
        } else if (code == 40) {
            $("#0_0").focus();
        }
    }
}

function initId() {
    var typeArray = new Array('input', 'select', 'select', 'select', 'select', 'a', 'a', 'a');
    var nameArray = new Array('vocabulary', 'character', 'property', 'position1', 'position2', 'icoarrowup', 'icoarrowdown', 'icodel');
    for (var i = 0; i < nameArray.length; i++) {
        var index = 0;
        $(typeArray[i] + "[name='" + nameArray[i] + "']").each(function () {
            $(this).attr("id", ++index + "_" + (i + 1));
        });
    }

    lastId = index + "_1";
}

function changeSource(index) {
    var preId = index + 1;
    while ($("#" + preId + "_9").val() != undefined) {
        $("#" + preId + "_9").val($("#" + index + "_9").val());
        preId++;
    }
}
